/*
-----------------------------------------------------------------------------
Filename:    TutorialApplication.cpp
-----------------------------------------------------------------------------

This source file is part of the
   ___                 __    __ _ _    _ 
  /___\__ _ _ __ ___  / / /\ \ (_) | _(_)
 //  // _` | '__/ _ \ \ \/  \/ / | |/ / |
/ \_// (_| | | |  __/  \  /\  /| |   <| |
\___/ \__, |_|  \___|   \/  \/ |_|_|\_\_|
      |___/                              
      Tutorial Framework
      http://www.ogre3d.org/tikiwiki/
-----------------------------------------------------------------------------
*/
#include "stdafx.h"

#include "TutorialApplication.h"

#include "Entity\Game\Predator.h"
#include "Entity\Game\Prey.h"


//-------------------------------------------------------------------------------------
TutorialApplication::TutorialApplication(void)
{
}
//-------------------------------------------------------------------------------------
TutorialApplication::~TutorialApplication(void)
{
}

//-------------------------------------------------------------------------------------
void TutorialApplication::createScene(void)
{
	// Set the scene's ambient light
	mSceneMgr->setAmbientLight(Ogre::ColourValue(0.5f, 0.5f, 0.5f));
	

	//Predator
	Predator* pPredator = new Predator();
	
	Ogre::ManualObject* predator = mSceneMgr->createManualObject("predator");
	predator->begin("BaseWhiteNoLighting", Ogre::RenderOperation::OT_LINE_STRIP);

	predator->colour(Ogre::ColourValue::Red);

	// define vertex position of index 0..3
	predator->position(20.0, 0.0, 0.0);
	predator->position(-20.0, 20.0, 0.0);
	predator->position(-20.0, -20.0, 0.0);

	// define usage of vertices by refering to the indexes
	predator->index(0);
	predator->index(1);
	predator->index(2);
	predator->index(0);
	
	predator->end();
	mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(predator);

	predator->getParentSceneNode()->setPosition(predator->getParentSceneNode()->getPosition() + Ogre::Vector3(-100, 0, 0));

	//Prey
	Prey* pPrey = new Prey();

	Ogre::ManualObject* prey = mSceneMgr->createManualObject("prey");
	prey->begin("BaseWhiteNoLighting", Ogre::RenderOperation::OT_LINE_STRIP);

	prey->colour(Ogre::ColourValue::Blue);

	// define vertex position of index 0..3
	prey->position(-20.0, 0.0, 0.0);
	prey->position(20.0, -20.0, 0.0);
	prey->position(20.0, 20.0, 0.0);

	// define usage of vertices by refering to the indexes
	prey->index(0);
	prey->index(1);
	prey->index(2);
	prey->index(0);
	
	prey->end();
	mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(prey);

	prey->getParentSceneNode()->setPosition(prey->getParentSceneNode()->getPosition() + Ogre::Vector3(100, 0, 0));
}

//-------------------------------------------------------------------------------------
void TutorialApplication::createViewports(void)
{
	// Create one viewport, entire window
	Ogre::Viewport* vp = mWindow->addViewport(mCamera);

	// black background
	vp->setBackgroundColour(Ogre::ColourValue(0, 0, 0));

	// Alter the camera aspect ratio to match the viewport
	mCamera->setAspectRatio(Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()));
}

//-------------------------------------------------------------------------------------
void TutorialApplication::createCamera(void)
{
	// create the camera
	mCamera = mSceneMgr->createCamera("PlayerCam");

	// set its position, direction  
	mCamera->setPosition(Ogre::Vector3(0, 0, 500));
	mCamera->lookAt(Ogre::Vector3(0, 0, 0));

	// set the near clip distance
	mCamera->setNearClipDistance(5);

	// create a default camera controller
	mCameraMan = new OgreBites::SdkCameraMan(mCamera);
}

//-------------------------------------------------------------------------------------
bool TutorialApplication::processUnbufferedInput(const Ogre::FrameEvent& evt)
{
	return true;
}

//-------------------------------------------------------------------------------------
bool TutorialApplication::frameRenderingQueued(const Ogre::FrameEvent& evt)
{
	bool ret = BaseApplication::frameRenderingQueued(evt);

	if (!processUnbufferedInput(evt))
	{
		return false;
	}
	
	return ret;
}

//-------------------------------------------------------------------------------------
bool TutorialApplication::keyPressed(const OIS::KeyEvent &arg)
{
	if (arg.key == OIS::KC_ESCAPE)
	{
		mShutDown = true;
	}

	mCameraMan->injectKeyDown(arg);

	return true;
}

//-------------------------------------------------------------------------------------
bool TutorialApplication::keyReleased(const OIS::KeyEvent &arg)
{
	return true;
}

//-------------------------------------------------------------------------------------
bool TutorialApplication::mouseMoved(const OIS::MouseEvent &arg)
{
	return true;
}

//-------------------------------------------------------------------------------------
bool TutorialApplication::mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
	return true;
}

//-------------------------------------------------------------------------------------
bool TutorialApplication::mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
	return true;
}

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
    INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
    int main(int argc, char *argv[])
#endif
    {
        // Create application object
        TutorialApplication app;

        try {
            app.go();
        } catch( Ogre::Exception& e ) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
            MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
            std::cerr << "An exception has occured: " <<
                e.getFullDescription().c_str() << std::endl;
#endif
        }

        return 0;
    }

#ifdef __cplusplus
}
#endif
